﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using System.ComponentModel;
using System.Text;
using SilverlightCommands;
using SilverlightMenu;
using SilverlightMenu.Library;

namespace SilverlightMenu.ViewModels
{
    public class MenuTestViewModel : INotifyPropertyChanged
    {
        MenuItem mvvmMenuItem;
        string imagesPath = "../Images/";
        public MenuTestViewModel()
        {
            mvvmMenuItem = new MenuItem()
            {
                Name = "Root"
            };

            var mnuFile = new MenuItem() { Name = "mnuFile", Text = "File" };
            var mnuEdit = new MenuItem() { Name = "mnuEdit", Text = "Edit" };
            var mnuWindow = new MenuItem() { Name = "mnuWindow", Text = "Window" };
            var mnuHelp = new MenuItem() { Name = "mnuHelp", Text = "Help" };

            var mnuNew = new MenuItem() { Name = "mnuNew", Text = "New" };
            var mnuSeparator1 = new MenuItem() { Name = "mnuSeparator1", Text = "-" };
            var mnuOpenFile = new MenuItem() { Name = "mnuOpenFile", Text = "Open File" };
            var mnuSaveFile = new MenuItem() { Name = "mnuSave", Text = "Save File", IsEnabled = false };
            var mnuCloseFile = new MenuItem() { Name = "mnuClose", Text = "Close File", IsEnabled = false };
            var mnuSeparator2 = new MenuItem() { Name = "mnuSeparator2", Text = "-" };
            var mnuExit = new MenuItem() { Name = "mnuExit", Text = "Exit" };

            var mnuNewFile = new MenuItem() { Name = "mnuNewFile", Text = "New File" };
            var mnuNewProject = new MenuItem() { Name = "mnuNewProject", Text = "New Project" };
            var mnuNewSolution = new MenuItem() { Name = "mnuNewSolution", Text = "New Solution" };

            var mnuCut = new MenuItem() { Name = "mnuCut", Text = "Cut" };
            var mnuCopy = new MenuItem() { Name = "mnuCopy", Text = "Copy" };
            var mnuPaste = new MenuItem() { Name = "mnuPaste", Text = "Paste" };
            var mnuDelete = new MenuItem() { Name = "mnuDelete", Text = "Delete" };

            var mnuWindow1 = new MenuItem() { Name = "mnuWindow1", Text = "Window 1", IsChecked = true, IsCheckable = true };
            var mnuWindow2 = new MenuItem() { Name = "mnuWindow2", Text = "Window 2", IsChecked = false, IsCheckable = true };
            var mnuWindow3 = new MenuItem() { Name = "mnuWindow3", Text = "Window 3", IsChecked = false, IsCheckable = true };

            var mnuAbout = new MenuItem() { Name = "mnuViewHelp", Text = "About Silverlight Menu" };

            mnuNew.Add(mnuNewFile);
            mnuNew.Add(mnuNewProject);
            mnuNew.Add(mnuNewSolution);

            mnuFile.Add(mnuNew);
            mnuFile.Add(mnuSeparator1);
            mnuFile.Add(mnuOpenFile);
            mnuFile.Add(mnuSaveFile);
            mnuFile.Add(mnuCloseFile);
            mnuFile.Add(mnuSeparator2);
            mnuFile.Add(mnuExit);

            mnuEdit.Add(mnuCut);
            mnuEdit.Add(mnuCopy);
            mnuEdit.Add(mnuPaste);
            mnuEdit.Add(mnuDelete);

            mnuWindow.Add(mnuWindow1);
            mnuWindow.Add(mnuWindow2);
            mnuWindow.Add(mnuWindow3);

            mnuHelp.Add(mnuAbout);

            mvvmMenuItem.Add(mnuFile);
            mvvmMenuItem.Add(mnuEdit);
            mvvmMenuItem.Add(mnuWindow);
            mvvmMenuItem.Add(mnuHelp);
        }

        public MenuItem MVVMMenuItem
        {
            get
            {
                return mvvmMenuItem;
            }
            set
            {
                mvvmMenuItem = value;
                OnPropertyChanged("MVVMMenuItem");
            }
        }

        public string ImagesPath
        {
            get
            {
                return imagesPath;
            }
            set
            {
                imagesPath = value;
                OnPropertyChanged("ImagesPath");
            }
        }

        public ICommand MenuCommand
        {
            get { return new RelayCommand(p => DoMenuCommand(p)); }
        }

        public void DoMenuCommand(object param)
        {
            var menuItem = (MenuItem)param;

            if (menuItem.IsCheckable)
                MessageBox.Show(string.Format("{0} is now {1}", menuItem.Name, menuItem.IsChecked ? "CHECKED" : "UNCHECKED"));
            else
                MessageBox.Show(string.Format("You clicked: {0}", menuItem.Name));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
